/*
 * Copyright 2011 OpenAjax Alliance
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* ---------------------------------------------------------------- */
/*                  Initialize namespace properties                 */
/* ---------------------------------------------------------------- */

/**
 * @namespace OAA_CI
 */

var OAA_CI = OAA_CI || {};

/**
 * @namespace OAA_CI.lists
 */

OAA_CI.lists = OAA_CI.lists || {};

OAA_CI.lists.treeView = new OAA_CI.SidebarView('ID_TREECHILDREN_LISTS_TREE');
OAA_CI.lists.listView = new OAA_CI.SidebarView('ID_TREECHILDREN_LISTS_LIST');
OAA_CI.lists.views = [ OAA_CI.lists.treeView, OAA_CI.lists.listView ];

OAA_CI.lists.hideMeter = function () { OAA_CI.lists.meter.hidden = true; };
OAA_CI.lists.showMeter = function () { OAA_CI.lists.meter.hidden = false; };

OAA_CI.sidebar = OAA_CI.lists;

/**
 * @function getDisplayProperties
 *
 * @desc Add getDisplayProperties method to cache elements;
 * called by populateTreeAs... functions.
 *
 * @return List of property values to be displayed in the sidebar
 *
 * @private
 */

OpenAjax.a11y.cache.ListElement.prototype.getDisplayProperties = function () {
  var de = this.dom_element;
  var text_content = de.getText();  
  return [de.tag_name, "", text_content, de.getAccessibility()];
};

OpenAjax.a11y.cache.ContainerElement.prototype.getDisplayProperties = function () {
  var de = this.dom_element;
  var number_of_list_elements = this.child_cache_elements.length;
  return [de.tag_name, number_of_list_elements, "", de.getAccessibility()];
};

OpenAjax.a11y.cache.LandmarkElement.prototype.getDisplayProperties = function () {
  var de = this.dom_element;
  return [de.tag_name, this.dom_element.role, this.label, de.getAccessibility()];
};

/**
 * @function getChildren
 *
 * @desc Add getChildren method to cache elements; called by
 * populateTreeAsHierarchy function.
 *
 * @return List of child elements
 *
 * @private
 */

OpenAjax.a11y.cache.ListElement.prototype.getChildren = function () {
  return this.child_cache_elements;
};

OpenAjax.a11y.cache.ContainerElement.prototype.getChildren = function () {
  return this.child_cache_elements;
};

OpenAjax.a11y.cache.LandmarkElement.prototype.getChildren = function () {
  return this.child_cache_elements;
};

/**
 * @function onLoad
 *
 * @memberOf OAA_CI.lists
 *
 * @desc Initialize sidebar and populate its default tabpanel.
 */

OAA_CI.lists.onLoad = function () {

  var config = {
   rules      : true,
   attributes : true,
   properties : true,
   style      : false,
   events     : true
  };

  var pt = new OAA_CI.Properties.PropertiesTabPanel("ID_PROPERTIES", config);
  OAA_CI.lists.treeView.addProperties(pt);  
  OAA_CI.lists.listView.addProperties(pt);  

  OAA_CI.lists.nls_strings = document.getElementById("ID_STRINGBUNDLE_LISTS");
  OAA_CI.lists.meter = document.getElementById('ID_METER_LISTS');

  OAA_CI.lists.treeView.isInitialized = false;
  OAA_CI.lists.treeView.showCacheItems();

};

/**
 * @function treeView.showCacheItems
 *
 * @memberOf OAA_CI.lists
 *
 * @desc Respond to the Tree View button in the sidebar's main tab panel.
 *       If the panel has not been initialized, populate it with list
 *       elements structured as a hierarchical list. Update properties
 *       display and page highlighting based on selection state of panel.
 */

OAA_CI.lists.treeView.showCacheItems = function () {
  var view = OAA_CI.lists.treeView;

  if (!view.isInitialized) {
    // Save references
    view.tab   = document.getElementById('ID_TAB_LISTS_TREE');
    view.tree  = document.getElementById('ID_TREE_LISTS_TREE');
    view.cache = OAA_CI.ruleset.dom_cache.lists_cache;

    // Populate the tabpanel
    var list_elements = view.cache.child_cache_elements;
    OAA_CI.populateTreeAsHierarchy(list_elements, 'ID_TREECHILDREN_LISTS_TREE');
    view.isInitialized = true;
  }

  view.updateItemSelectionDisplay();
};

/**
 * @function listView.showCacheItems
 *
 * @memberOf OAA_CI.lists
 *
 * @desc Respond to the List View button in the sidebar's main tab panel.
 *       If the panel has not been initialized, populate it with list
 *       elements structured as a flat list. Update properties display
 *       and page highlighting based on selection state of panel.
 */

OAA_CI.lists.listView.showCacheItems = function () {
  var view = OAA_CI.lists.listView;

  if (!view.isInitialized) {
    // Save references
    view.tab   = document.getElementById('ID_TAB_LISTS_LIST');
    view.tree  = document.getElementById('ID_TREE_LISTS_LIST');
    view.cache = OAA_CI.ruleset.dom_cache.lists_cache;

    // Populate the tabpanel
    var list_elements = view.cache.container_elements;
    OAA_CI.populateTreeAsHierarchy(list_elements, 'ID_TREECHILDREN_LISTS_LIST');
    view.isInitialized = true;
  }

  view.updateItemSelectionDisplay();
};

/**
 * @function onItemSelectionChange
 *
 * @memberOf OAA_CI.lists
 *
 * @desc Handle the onselect event from the XUL tree in the main tab panel
 *       by calling the updateItemSelectionDisplay method.
 *
 * @param {Number} index - Specifies the view that triggered the onselect event.
 */

OAA_CI.lists.onItemSelectionChange = function (index) {

  var view = OAA_CI.lists.views[index];
  view.updateItemSelectionDisplay();

};
